# -*- coding: utf-8 -*-

# Bliotecas padrões
from django.forms import *
from django.forms.extras.widgets import SelectDateWidget
from django.forms.widgets import PasswordInput 
from django.contrib.admin.widgets import AdminFileWidget,AdminTimeWidget
from django.contrib.admin import widgets

# Models
from myproject.cadastro.models import *
from myproject.aula.models import *

# Bibliotecas externas

# Bibliotecas das apps
from choices import *
from extras import SelectTimeWidget,AdminImageWidget

class EsqueceuSenhaModelForm(forms.Form):
    email = forms.EmailField()
    login = forms.CharField(label='Login',widget = forms.TextInput(attrs={'maxlength':'16','size':'14','onkeyup':u'saltaCampo(this);'}))
    
class AlterarSenhaModelForm(forms.Form):
    login = forms.CharField(label='Login',widget = forms.TextInput(attrs={'maxlength':'16','size':'14','onkeyup':u'saltaCampo(this);'}))
    senha = forms.CharField(label='Senha',widget=PasswordInput(attrs={'maxlength':'10','size':'4','onkeyup':u'saltaCampo(this);'}))
    senhanova = forms.CharField(label='Nova senha',widget=PasswordInput(attrs={'maxlength':'10','size':'4','onkeyup':u'saltaCampo(this);'}))

class AlunosModelForm(ModelForm):
	
	class Meta:
		model = Alunos
		fields = ('status','login','senha','nome','sobrenome','dataNascimento','telefone','celular','rg','cpf',\
				  'email','cep','endereco','bairro','cidade','estado','pais','curso','foto')
	
	def __init__(self, *args, **kwargs):
		super(AlunosModelForm, self).__init__(*args, **kwargs)
		self.fields['dataNascimento'].widget = SelectDateWidget(years=range(ANO_DE,ANO_ATE))
		self.fields['login'].widget = forms.TextInput(attrs={'maxlength':'16','size':'14','onkeyup':u'saltaCampo(this);'})
		self.fields['email'].widget = forms.TextInput(attrs={'maxlength':'32','size':'30','onkeyup':u'saltaCampo(this);'})
		self.fields['nome'].widget = forms.TextInput(attrs={'maxlength':'14','size':'11','onkeyup':u'saltaCampo(this);'})
		self.fields['sobrenome'].widget = forms.TextInput(attrs={'maxlength':'16','size':'14','onkeyup':u'saltaCampo(this);'})
		self.fields['senha'].widget=PasswordInput(attrs={'maxlength':'10','size':'4','onkeyup':u'saltaCampo(this);'})
		self.fields['rg'].widget = forms.TextInput(attrs={'maxlength':'9','size':'7','onkeyup':u'saltaCampo(this);'})
		self.fields['endereco'].widget = forms.TextInput(attrs={'maxlength':'64','size':'60','onkeyup':u'saltaCampo(this);'})
		self.fields['bairro'].widget = forms.TextInput(attrs={'maxlength':'16','size':'14','onkeyup':u'saltaCampo(this);'})
		self.fields['cidade'].widget = forms.TextInput(attrs={'maxlength':'16','size':'14','onkeyup':u'saltaCampo(this);'})
		self.fields['telefone'] = BRPhoneNumberField(label=u'Telefone',widget=forms.TextInput(attrs={'maxlength':"14",'size':"11",'onkeypress':"return digitos(event, this);",'onkeyup':u"Mascara(3,this,event);"}))
		self.fields['celular'] = BRPhoneNumberField(label=u'Celular',widget=forms.TextInput(attrs={'maxlength':"14",'size':"11",'onkeypress':"return digitos(event, this);",'onkeyup':u"Mascara(3,this,event);"}))
		self.fields['cpf'] = BRCPFField(required=False,label=u'CPF',widget=forms.TextInput(attrs={'maxlength':"14",'size':"11",'onkeypress':"return digitos(event, this);",'onkeyup':u"Mascara(1,this,event);"}))
		self.fields['cep'] = BRZipCodeField(label=u'Cep',widget = forms.TextInput(attrs={'maxlength':"9",'size':"7",'onkeypress':'return digitos(event, this);','onkeyup':u'Mascara(5,this,event);'}))
		self.fields['foto'].widget = AdminImageWidget()

class AlunosRespModelForm(ModelForm):
	class Meta:
		model = Alunos
		fields = ('status','login','senha','nome','sobrenome','dataNascimento','telefone','celular','rg','cpf',
				  'email','cep','endereco','bairro','cidade','estado','pais','curso','foto',
				  'nomeResponsavel','sobrenomeResponsavel','dataNascimentoResponsavel',
				  'telefoneResponsavel','rgResponsavel','cpfResponsavel','emailResponsavel',
				  'cepResponsavel','enderecoResponsavel','bairroResponsavel','cidadeResponsavel',
				  'estadoResponsavel','paisResponsavel')        
	def __init__(self, *args, **kwargs):
		super(AlunosRespModelForm, self).__init__(*args, **kwargs)
		self.fields['dataNascimento'].widget = SelectDateWidget(years=range(ANO_DE,ANO_ATE))
		self.fields['login'].widget = forms.TextInput(attrs={'maxlength':'16','size':'14','onkeyup':u'saltaCampo(this);'})
		self.fields['email'].widget = forms.TextInput(attrs={'maxlength':'32','size':'30','onkeyup':u'saltaCampo(this);'})
		self.fields['nome'].widget = forms.TextInput(attrs={'maxlength':'14','size':'11','onkeyup':u'saltaCampo(this);'})
		self.fields['sobrenome'].widget = forms.TextInput(attrs={'maxlength':'16','size':'14','onkeyup':u'saltaCampo(this);'})
		self.fields['senha'].widget=PasswordInput(attrs={'maxlength':'10','size':'4','onkeyup':u'saltaCampo(this);'})
		self.fields['rg'].widget = forms.TextInput(attrs={'maxlength':'9','size':'7','onkeyup':u'saltaCampo(this);'})
		self.fields['endereco'].widget = forms.TextInput(attrs={'maxlength':'64','size':'60','onkeyup':u'saltaCampo(this);'})
		self.fields['bairro'].widget = forms.TextInput(attrs={'maxlength':'16','size':'14','onkeyup':u'saltaCampo(this);'})
		self.fields['cidade'].widget = forms.TextInput(attrs={'maxlength':'16','size':'14','onkeyup':u'saltaCampo(this);'})
		self.fields['telefone'] = BRPhoneNumberField(label=u'Telefone',widget=forms.TextInput(attrs={'maxlength':"14",'size':"11",'onkeypress':"return digitos(event, this);",'onkeyup':u"Mascara(3,this,event);"}))
		self.fields['celular'] = BRPhoneNumberField(label=u'Celular',widget=forms.TextInput(attrs={'maxlength':"14",'size':"11",'onkeypress':"return digitos(event, this);",'onkeyup':u"Mascara(3,this,event);"}))
		self.fields['cpf'] = BRCPFField(required=False,label=u'CPF',widget=forms.TextInput(attrs={'maxlength':"14",'size':"11",'onkeypress':"return digitos(event, this);",'onkeyup':u"Mascara(1,this,event);"}))
		self.fields['cep'] = BRZipCodeField(label=u'Cep',widget = forms.TextInput(attrs={'maxlength':"9",'size':"7",'onkeypress':'return digitos(event, this);','onkeyup':u'Mascara(5,this,event);'}))        
		self.fields['foto'].widget = AdminImageWidget()
		
		self.fields['dataNascimentoResponsavel'].widget = SelectDateWidget(years=range(ANO_DE,ANO_ATE))
		self.fields['emailResponsavel'].widget = forms.TextInput(attrs={'maxlength':'32','size':'30','onkeyup':u'saltaCampo(this);'})
		self.fields['nomeResponsavel'].widget = forms.TextInput(attrs={'maxlength':'14','size':'11','onkeyup':u'saltaCampo(this);'})
		self.fields['sobrenomeResponsavel'].widget = forms.TextInput(attrs={'maxlength':'16','size':'14','onkeyup':u'saltaCampo(this);'})
		self.fields['rgResponsavel'].widget = forms.TextInput(attrs={'maxlength':'9','size':'7','onkeyup':u'saltaCampo(this);'})
		self.fields['enderecoResponsavel'].widget = forms.TextInput(attrs={'maxlength':'64','size':'60','onkeyup':u'saltaCampo(this);'})
		self.fields['bairroResponsavel'].widget = forms.TextInput(attrs={'maxlength':'16','size':'14','onkeyup':u'saltaCampo(this);'})
		self.fields['cidadeResponsavel'].widget = forms.TextInput(attrs={'maxlength':'16','size':'14','onkeyup':u'saltaCampo(this);'})
		self.fields['telefoneResponsavel'] = BRPhoneNumberField(label=u'Telefone',widget=forms.TextInput(attrs={'maxlength':"14",'size':"11",'onkeypress':"return digitos(event, this);",'onkeyup':u"Mascara(3,this,event);"}))
		self.fields['cpfResponsavel'] = BRCPFField(required=False,label=u'CPF',widget=forms.TextInput(attrs={'maxlength':"14",'size':"11",'onkeypress':"return digitos(event, this);",'onkeyup':u"Mascara(1,this,event);"}))
		self.fields['cepResponsavel'] = BRZipCodeField(label=u'Cep',widget = forms.TextInput(attrs={'maxlength':"9",'size':"7",'onkeypress':"return digitos(event, this);",'onkeyup':u"Mascara(5,this,event);"}))

class AulaAlunoModelForm(ModelForm):
	
	class Meta:
	    model = AulaAluno
	    fields = ('aluno','curso','aula','data','horaInicial','horaFinal')
	
	def __init__(self, *args, **kwargs):
		super(AulaAlunoModelForm, self).__init__(*args, **kwargs)
		self.fields['data'].widget = SelectDateWidget(years=range(ANO_DE,ANO_ATE))
		self.fields['horaInicial'].widget = AdminTimeWidget()
		self.fields['horaFinal'].widget = AdminTimeWidget()
		campo = getattr(self, 'instance', None)
		self.fields['aluno'] = forms.ModelChoiceField(label='Aluno', queryset=Alunos.objects.filter(id=campo.aluno.id,status=True))
		self.fields['curso'] = forms.ModelChoiceField(label='Curso', queryset=Curso.objects.filter(id=campo.curso.id))
		self.fields['aula'] = forms.ModelChoiceField(label='Aula', queryset=Aula.objects.filter(curso__id=campo.curso.id))
	
class ProfessorModelForm(ModelForm):
	class Meta:
		model = Professor
		fields = ('status','login','senha','nome','sobrenome','dataNascimento','telefone','celular','rg','cpf','email','cep','endereco','bairro','cidade','estado','pais','curso','curriculo','foto')
	def __init__(self, *args, **kwargs):
		super(ProfessorModelForm, self).__init__(*args, **kwargs)
		self.fields['dataNascimento'].widget = SelectDateWidget(years=range(ANO_DE,ANO_ATE))
		self.fields['login'].widget = forms.TextInput(attrs={'maxlength':'16','size':'14','onkeyup':u'saltaCampo(this);'})
		self.fields['email'].widget = forms.TextInput(attrs={'maxlength':'32','size':'30','onkeyup':u'saltaCampo(this);'})
		self.fields['nome'].widget = forms.TextInput(attrs={'maxlength':'14','size':'11','onkeyup':u'saltaCampo(this);'})
		self.fields['sobrenome'].widget = forms.TextInput(attrs={'maxlength':'16','size':'14','onkeyup':u'saltaCampo(this);'})
		self.fields['senha'].widget=PasswordInput(attrs={'maxlength':'10','size':'4','onkeyup':u'saltaCampo(this);'})
		self.fields['rg'].widget = forms.TextInput(attrs={'maxlength':'9','size':'7','onkeyup':u'saltaCampo(this);'})
		self.fields['endereco'].widget = forms.TextInput(attrs={'maxlength':'64','size':'60','onkeyup':u'saltaCampo(this);'})
		self.fields['bairro'].widget = forms.TextInput(attrs={'maxlength':'16','size':'14','onkeyup':u'saltaCampo(this);'})
		self.fields['cidade'].widget = forms.TextInput(attrs={'maxlength':'16','size':'14','onkeyup':u'saltaCampo(this);'})
		self.fields['telefone'] = BRPhoneNumberField(label=u'Telefone',widget=forms.TextInput(attrs={'maxlength':"14",'size':"11",'onkeypress':"return digitos(event, this);",'onkeyup':u"Mascara(3,this,event);"}))
		self.fields['celular'] = BRPhoneNumberField(label=u'Celular',widget=forms.TextInput(attrs={'maxlength':"14",'size':"11",'onkeypress':"return digitos(event, this);",'onkeyup':u"Mascara(3,this,event);"}))
		self.fields['cpf'] = BRCPFField(required=False,label=u'CPF',widget=forms.TextInput(attrs={'maxlength':"14",'size':"11",'onkeypress':"return digitos(event, this);",'onkeyup':u"Mascara(1,this,event);"}))
		self.fields['cep'] = BRZipCodeField(label=u'Cep',widget = forms.TextInput(attrs={'maxlength':"9",'size':"7",'onkeypress':"return digitos(event, this);",'onkeyup':u"Mascara(5,this,event);"}))
		self.fields['curriculo'].widget = AdminFileWidget()
		self.fields['foto'].widget = AdminImageWidget()

class CursosProfessorModelForm(ModelForm):
	class Meta:
		model = CursosProfessor

class AdministradorModelForm(ModelForm):
	class Meta:
		model = Administrador
		fields = ('status','login','senha','nome','sobrenome','dataNascimento','telefone','celular','rg','cpf','email','cep','endereco','bairro','cidade','estado','pais')        
	def __init__(self, *args, **kwargs):
		super(AdministradorModelForm, self).__init__(*args, **kwargs)
		self.fields['dataNascimento'].widget = SelectDateWidget(years=range(ANO_DE,ANO_ATE))
		self.fields['login'].widget = forms.TextInput(attrs={'maxlength':'16','size':'14','onkeyup':u'saltaCampo(this);'})
		self.fields['email'].widget = forms.TextInput(attrs={'maxlength':'32','size':'30','onkeyup':u'saltaCampo(this);'})
		self.fields['nome'].widget = forms.TextInput(attrs={'maxlength':'14','size':'11','onkeyup':u'saltaCampo(this);'})
		self.fields['sobrenome'].widget = forms.TextInput(attrs={'maxlength':'16','size':'14','onkeyup':u'saltaCampo(this);'})
		self.fields['senha'].widget=PasswordInput(attrs={'maxlength':'10','size':'4','onkeyup':u'saltaCampo(this);'})
		self.fields['rg'].widget = forms.TextInput(attrs={'maxlength':'9','size':'7','onkeyup':u'saltaCampo(this);'})
		self.fields['endereco'].widget = forms.TextInput(attrs={'maxlength':'64','size':'60','onkeyup':u'saltaCampo(this);'})
		self.fields['bairro'].widget = forms.TextInput(attrs={'maxlength':'16','size':'14','onkeyup':u'saltaCampo(this);'})
		self.fields['cidade'].widget = forms.TextInput(attrs={'maxlength':'16','size':'14','onkeyup':u'saltaCampo(this);'})
		self.fields['telefone'] = BRPhoneNumberField(label=u'Telefone',widget=forms.TextInput(attrs={'maxlength':"14",'size':"11",'onkeypress':"return digitos(event, this);",'onkeyup':u"Mascara(3,this,event);"}))
		self.fields['celular'] = BRPhoneNumberField(label=u'Celular',widget=forms.TextInput(attrs={'maxlength':"14",'size':"11",'onkeypress':"return digitos(event, this);",'onkeyup':u"Mascara(3,this,event);"}))
		self.fields['cpf'] = BRCPFField(required=False,label=u'CPF',widget=forms.TextInput(attrs={'maxlength':"14",'size':"11",'onkeypress':"return digitos(event, this);",'onkeyup':u"Mascara(1,this,event);"}))
		self.fields['cep'] = BRZipCodeField(label=u'Cep',widget = forms.TextInput(attrs={'maxlength':"9",'size':"7",'onkeypress':"return digitos(event, this);",'onkeyup':u"Mascara(5,this,event);"}))

class PresencaModelForm(ModelForm):
	
	class Meta:
	    model = Presenca
	    fields = ('aluno','curso','aula','presenca')
	
	def __init__(self, *args, **kwargs):
	    super(PresencaModelForm, self).__init__(*args, **kwargs)
	    campo = getattr(self, 'instance', None)
	    self.fields['aluno'] = forms.ModelChoiceField(label='Aluno', queryset=Alunos.objects.filter(id=campo.aluno.id,status=True))
	    self.fields['curso'] = forms.ModelChoiceField(label='Curso', queryset=Curso.objects.filter(id=campo.curso.id))
	    self.fields['aula'] = forms.ModelChoiceField(label='Aula', queryset=Aula.objects.filter(id=campo.aula.id))

class NotaModelForm(ModelForm):
	
	class Meta:
	    model = Nota
	    fields = ('aluno','curso','descricao','notaAluno','notaTotal')
	
	def __init__(self, *args, **kwargs):
		try:
			cod_aluno = kwargs['cod_aluno']
		except:
			cod_aluno = None
		try:
			cod_curso = kwargs['cod_curso']
			kwargs = {}
		except:
			cod_curso = None
		super(NotaModelForm, self).__init__(*args, **kwargs)
		self.fields['descricao'].widget = forms.TextInput(attrs={'maxlength':'32','size':'40'})
		self.fields['notaAluno'].widget = forms.TextInput(attrs={'maxlength':'5','size':'7'})
		self.fields['notaTotal'].widget = forms.TextInput(attrs={'maxlength':'5','size':'7'})
		instance = getattr(self, 'instance', None)
		if instance.id:
			self.fields['aluno'] = ModelChoiceField(label='Aluno', queryset=Alunos.objects.filter(id=instance.aluno.id))
			self.fields['curso'] = ModelChoiceField(label='Curso', queryset=Curso.objects.filter(id=instance.curso.id))
		if cod_aluno:
			qs = Alunos.objects.filter(id=cod_aluno)
			print qs
			self.fields['aluno'] = ModelChoiceField(label='Aluno', queryset=qs,initial=qs[0])
		if cod_curso:
			qs = Curso.objects.filter(id=cod_curso)
			self.fields['curso'] = ModelChoiceField(label='Curso', queryset=qs,initial=qs[0])
			
class RelNotaForm(forms.Form):	
	
	aluno = ModelChoiceField(required=False, label='Aluno', queryset=Alunos.objects.filter())
	curso = ModelChoiceField(required=False, label='Curso', queryset=Curso.objects.filter())
	
class RelHistoricoForm(forms.Form):	
	
	aluno = ModelChoiceField(required=False, label='Aluno', queryset=Alunos.objects.filter())
	curso = ModelChoiceField(required=False, label='Curso', queryset=Curso.objects.filter())	